from abc import abstractmethod


class BaseDbExecutor:

    @abstractmethod
    def execute_sql(self, sql: str, params: tuple = None):
        """
        执行SQL
        :param sql:
        :param params:
        :return:
        """
        pass

    @abstractmethod
    def batch_insert_data(self, insert_sql: str, data_list: list):
        """
        批量插入数据
        :param insert_sql: 插入数据的sql
        :param data_list: 批量插入的数据
        """
        pass

    @abstractmethod
    def update_sql(self, update_sql: str, params: tuple = None):
        """
        执行更新SQL
        :param update_sql:
        :param params:
        :return:
        """
        pass

    @abstractmethod
    def execute_query(self, sql: str, params: tuple):
        """
        执行查询SQL
        :param sql:
        :param params:
        :return:
        """
        pass


class SqlExecutor:
    """
    数据库执行器
    """
    def __init__(self):
        from utils.MysqlExecutor import mysql_enabled
        if mysql_enabled():
            from utils.MysqlExecutor import get_mysql_config, MySQLConnectionPool
            _mysql_config = get_mysql_config()
            self.val_placeholder = '%s'
            self.db_instance = MySQLConnectionPool(
                host=_mysql_config['host'],
                port=_mysql_config['port'],
                user=_mysql_config['user'],
                password=_mysql_config['password'],
                database=_mysql_config['database']
            )
        else:
            self.val_placeholder = '?'

